from flask import render_template,request
from app import db
from . import auth_user
from .model import User
from .service import UserService
import json


@auth_user.route('/save',methods=['GET','POST'])
def save():
    if request.method == 'POST':
        user_pk = request.form.get('user_pk', None)
        user_loginname = request.form.get('user_loginname', None)
        user_password = request.form.get('user_password', None)
        user_name = request.form.get('user_name', None)
        user_sex = request.form.get('user_sex', None)
        user_idcard = request.form.get('user_idcard', None)
        user_phone = request.form.get('user_phone', None)
        user_birthday = request.form.get('user_birthday', None)
        user_email = request.form.get('user_email', None)
        user_intime = request.form.get('user_intime', None)
        fk_role_pk = request.form.get('fk_role_pk', None)
        fk_organ_pk = request.form.get('fk_organ_pk', None)

        newobj = User()
        # newobj.user_pk=user_pk
        newobj.user_loginname = user_loginname
        newobj.user_password = user_password
        newobj.user_name = user_name
        newobj.user_sex = user_sex
        newobj.user_idcard = user_idcard
        newobj.user_phone = user_phone
        newobj.user_birthday = user_birthday
        newobj.user_email = user_email
        newobj.user_intime = user_intime
        newobj.fk_role_pk = fk_role_pk
        newobj.fk_organ_pk = fk_organ_pk
        user_status = request.form.get('user_status', None)
        UserService.save(newobj)
    data = {
        'msg':'保存成功',
        'code':0,
    }
    return json.dumps(data)


@auth_user.route('/update',methods=['GET','POST'])
def update():
    if request.method == 'POST':
        user_pk = request.form.get('user_pk', None)
        user_loginname = request.form.get('user_loginname', None)
        user_password = request.form.get('user_password', None)
        user_name = request.form.get('user_name', None)
        user_sex = request.form.get('user_sex', None)
        user_idcard = request.form.get('user_idcard', None)
        user_phone = request.form.get('user_phone', None)
        user_birthday = request.form.get('user_birthday', None)
        user_email = request.form.get('user_email', None)
        user_intime = request.form.get('user_intime', None)
        fk_role_pk = request.form.get('fk_role_pk', None)
        fk_organ_pk = request.form.get('fk_organ_pk', None)
        user_status = request.form.get('user_status', None)

        newobj = User()
        newobj.user_pk=user_pk
        newobj.user_loginname = user_loginname
        newobj.user_password = user_password
        newobj.user_name = user_name
        newobj.user_sex = user_sex
        newobj.user_idcard = user_idcard
        newobj.user_phone = user_phone
        newobj.user_birthday = user_birthday
        newobj.user_email = user_email
        newobj.user_intime = user_intime
        newobj.fk_role_pk = fk_role_pk
        newobj.fk_organ_pk = fk_organ_pk
        newobj.user_status = user_status
        UserService.update(newobj)
    data = {
        'msg':'保存成功',
        'code':0,
    }
    return json.dumps(data)


@auth_user.route('/list')
def select():
    users = UserService.lsit()
    data = {
        'msg':'success',
        'code':0,
        'data':User.to_dict(users)
    }
    return json.dumps(data)

#获取用户信息的分页列表
@auth_user.route('/pages',methods=['GET','POST'])
def pages():
    if request.method == "POST":
        fk_organ_pk = request.form.get('fk_organ_pk', None)
        user_name = request.form.get('user_name', None)
        page = request.form.get("page")  #获取页码
    else:
        fk_organ_pk = request.args.get('fk_organ_pk', None)
        user_name = request.args.get('user_name', None)
        page = request.args.get("page")
    user = User()
    user.fk_organ_pk= fk_organ_pk
    user.user_name= user_name
    
    pages = UserService.pages(user,page)
    data = {
        'msg':'success',
        'code':0,
        'data':User.to_dict(pages.items),
        'count':pages.total
    }

    print(pages.items)
    return json.dumps(data,ensure_ascii=False)  #关闭已ascii编码输出

#根据主键删除用户信息
@auth_user.route('/delete',methods=['GET','POST'])
def delete():
    if request.method == "POST":
        user_pk = request.form.get("user_pk")  #获取页码
    else:
        user_pk = request.args.get("user_pk")
    UserService.delete(user_pk)
    data = {
        'msg':'删除成功',
        'code':0,
    }
    return json.dumps(data,ensure_ascii=False)  #关闭已ascii编码输出

@auth_user.route('/upQTS',methods = ['GET','POST'])
def upQTS():
    if request.method == "POST":
        user_pk = request.form.get("user_pk")  #获取页码
        user_status = request.form.get("user_status")  #获取页码
    else:
        user_pk = request.args.get("user_pk")
        user_status = request.args.get("user_status")
    UserService.upQTS(user_pk,user_status)
    data = {
        'msg':'修改成功',
        'code':0,
    }
    return json.dumps(data,ensure_ascii=False)  #关闭已ascii编码输出
